package com.jimikongjian.controller;

import com.jimikongjian.models.Article;
import com.jimikongjian.models.ArticleRoom;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.models.User;
import com.jimikongjian.service.base.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by fuxiuyang on 17/2/27.
 * 文章只能创建和查询
 */
@RestController("articleController")
@RequestMapping(value = "/v1")
public class ArticleController {

    @Autowired
    @Qualifier("articleService")
    private IArticleService articleService;

    /**
     * 在空间内创建文章
     * @param article
     * @return
     */
    @RequestMapping(value = "/article", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage createArticle(@RequestBody Article article){

        return articleService.createArticle(article);
    }

    /**
     * 查询空间中的文章
     * @param start
     * @param length
     * @param orderBy
     * @param desc
     * @param userId
     * @param roomId
     * @param author
     * @param id
     * @return
     */
    @RequestMapping(value = "/article", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectArticle(@RequestParam(name = "start", defaultValue = "0") Integer start,
                                         @RequestParam(name = "length", defaultValue = "10") Integer length,
                                         @RequestParam(name = "orderBy", defaultValue = "0") Integer orderBy,
                                         @RequestParam(name = "desc", defaultValue = "1") Integer desc,
                                         @RequestParam(name = "userId", defaultValue = "-1") Integer userId,
                                         @RequestParam(name = "roomId", defaultValue = "-1") Integer roomId,
                                         @RequestParam(name = "author", defaultValue = "-1") Integer author,
                                         @RequestParam(name = "id", defaultValue = "-1") Integer id){

        if (roomId != null && roomId != -1 ){
            return articleService.selectArticleByRoomId(userId, roomId, orderBy, desc, start, length);
        }
        if (author != null && author != -1){
            //            这里的author应该是用户本身的id号
            return articleService.selectArticleByAuthor(userId, author, start, length);
        }
        if (id != null && id != -1){
            return articleService.selectArticleById(id);
        }

        return articleService.selectArticleByUserId(userId, orderBy, desc, start, length);
    }

    /**
     * 记录用户点击文章的行为
     * @param map
     */
    @RequestMapping(value = "/click", method = RequestMethod.POST)
    @ResponseBody
    public void clickArticle(@RequestBody Map<String, String> map){
        int id = -1;
        if (map.containsKey("id")){
            id = Integer.parseInt(map.get("id"));
            articleService.updateAddArticleClickCount(id);
        }
    }

}
